package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.commmon.CustomException;
import org.example.mapper.CategoryMapper;
import org.example.pojo.Category;
import org.example.pojo.Medicine;
import org.example.pojo.Combination;
import org.example.service.CategoryService;
import org.example.service.MedicineService;
import org.example.service.CombinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private MedicineService dishService;

    @Autowired
    private CombinationService combinationService;



    //根据id删除分类
    @Override
    public void remove(Long id) {
        //查询当前分类是否关联药品
        LambdaQueryWrapper<Medicine> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Medicine::getCategoryId,id);
        int count = dishService.count(dishLambdaQueryWrapper);
        //关联药品 抛出异常
        if (count>0) {
            throw new CustomException("当前分类下有药品关联，不能删除");
        }



        //查询当前分类是否关联组合
        LambdaQueryWrapper<Combination> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper.eq(Combination::getCategoryId,id);
        int count1 = combinationService.count(setMealLambdaQueryWrapper);
        if (count1>0){
            throw new CustomException("当前分类下有组合关联，不能删除");
        }

        super.removeById(id);
    }
}
